<template>
  <div>
    <vxe-modal
      v-model="visbled"
      :width="width"
      :height="height"
      show-footer
      :loading="loading"
      :id="rowId"
      :destroy-on-close="true"
    >
      <!-- 标题 -->
      <template #title>
        <slot slot="title">{{ title || `下一环节:${nextNode}` }}</slot>
      </template>
      <template #default>
        <slot
          name="default"
          :params="params"
          :visbled="visbled"
          :query="queryParams"
          :ndkp="ndkp"
          :nextNode="nextNode"
          :nextNodeDbrList="nextNodeDbrList"
          :save="save"
          :submit="submit"
          v-if="visbled"
        ></slot>
      </template>
      <template #footer>
        <slot name="footer">
          <vxe-button
            title="退回上一步"
            content="退回上一步"
            @click="backFlow"
            v-if="back"
          ></vxe-button>

          <vxe-button
            title="保存"
            status="primary"
            content="保存"
            @click="save"
          ></vxe-button>
          <vxe-button
            title="提交"
            status="primary"
            @click="submit"
            content="提交"
          ></vxe-button>
        </slot>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { flowArr } from "../../api/common";
import {
  queryJobByJobId,
  saveFlow,
  submitFlow,
  returnUpperFlowJob,
} from "../../api/yearCheck";
export default {
  name: "year-model",
  props: {
    width: {
      type: String,
      default() {
        return "85vw";
      },
    },
    height: {
      type: String,
      default() {
        return "90vh";
      },
    },
    title: {
      type: String,
      default() {
        return "添加";
      },
    },
    rowId: {
      type: String,
      default() {
        return "";
      },
    },
    axios: {
      type: Object,
      default() {
        return {
          query: null,
          save: null,
          submit: null,
        };
      },
    },
    toSubmit: {
      type: Boolean,
      default() {
        return false;
      },
    },
    refs: {
      type: Object,
    },
    back: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      visbled: false,
      params: {},
      queryParams: {},
      ndkp: {},
      nextNode: "", // 下环节名称
      jobid: "",
      nextNodeDbrList: [],
    };
  },

  mounted() {},

  methods: {
    handler(query, callback = null) {
      this.params = query;
      if (callback) {
        this.getQueryByFid(callback);
      } else {
        this.visbled = true;
      }
    },
    async getQueryByFid(callback) {
      this.loading = true;
      const { jobid } = callback;
      this.jobid = jobid;
      const res = await queryJobByJobId(callback);
      if (res.success) {
        this.queryParams = res.result;
        this.ndkp = res.result.ndkp;
        this.nextNodeDbrList = res.result.nextNodeDbrList;
        this.nextNode = res.result?res.result.nextNode:'';
        this.visbled = true;
        this.loading = false;
      } else {
        this.$VxeMessage({ content: res.meaasge, status: "error" });
        this.loading = false;
      }
    },
    // 保存
    save() {
      this.$emit("year-save", this.params);
      if (this.toSubmit) {
        this.loading = true;
        const { data, form } = this.getSaveSubmit();
        saveFlow(data).then((res) => {
          if (res.success) {
            //console.log(res);
            this.$VxeMessage({ content: res.meaasge, status: "success" });
            this.getQueryByFid({ jobid: this.jobid });
            this.loading = false;
          } else {
            this.$VxeMessage({ content: res.meaasge, status: "error" });
            this.loading = false;
          }
        });
      }
    },
    // 提交
    submit() {
      this.$emit("year-submit", this.params);
      if (this.toSubmit) {
        if (this.toSubmit) {
          const { data, form } = this.getSaveSubmit();
          form.validateFields((err, values) => {
            if (!err) {
              this.loading = true;
              submitFlow(data).then((res) => {
                if (res.success) {
                  //console.log(res);
                  this.$VxeMessage({ content: res.meaasge, status: "success" });
                  this.$emit("refresh");
                  setTimeout(() => {
                    this.loading = false;
                    this.visbled = false;
                  });
                } else {
                  this.$VxeMessage({ content: res.meaasge, status: "error" });
                  this.loading = false;
                }
              });
            }
          });
        }
      }
    },
    backFlow() {
      if (this.toSubmit && this.back) {
        const { data, form } = this.getSaveSubmit();
        if (data["fthyj"] == "") {
          this.$VxeMessage({ content: "请输入退回意见", status: "warning" });
          return;
        }
        returnUpperFlowJob({
          opinion: data.fthyj,
          jobid: data.jobid,
        }).then((res) => {
          if (res.success) {
            this.$VxeMessage({ content: res.meaasge, status: "success" });
            this.$emit("refresh");
            this.loading = false;
          } else {
            this.$VxeMessage({ content: res.meaasge, status: "error" });
            this.loading = false;
          }
        });
      }
    },
    getSaveSubmit() {
      const { params, dbrid, smgflag, opinion, form } = this.refs.form;
      const formData = {};
      formData.map = params;
      formData.jobid = this.jobid;
      formData.dbrid = dbrid;
      formData.smgflag = smgflag;
      formData.currentNode = this.params.fhjmc;
      formData.opinion = opinion;
      formData.ndkpid = this.params.ndkpid;
      return { data: formData, form };
    },
  },
  watch: {},
};
</script>

<style scoped></style>
